package net.huashitong.supervision.gongjian.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.gongjian.dao.ProjectFeedBackDao;
import net.huashitong.supervision.gongjian.dao.ProjectTaskDao;
import net.huashitong.supervision.gongjian.entity.ProjectFeedBackEntity;
import net.huashitong.supervision.gongjian.entity.ProjectTaskEntity;
import net.huashitong.supervision.gongjian.entity.project.*;
import net.huashitong.supervision.gongjian.filter.ProjectFeedBackFilter;
import net.huashitong.supervision.gongjian.filter.ProjectTaskFilter;
import net.huashitong.supervision.gongjian.service.project.*;
import net.huashitong.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ProjectTaskService extends StringPKService<ProjectTaskEntity> {
    @Autowired
    private ProjectTaskDao dao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private ProjectFeedBackDao feedBackdao;
    @Autowired
    private ZhengQianService zhengQianService;
    @Autowired
    private KaiGongService kaiGongService;
    @Autowired
    private ZaijianTisuService zaijianTisuService;
    @Autowired
    private JungongTouchanService jungongTouchanService;
    @Autowired
    private ZhaoShangProjectService zhaoshangService;

    @Override
    protected BaseDao<ProjectTaskEntity, String> getDao() {
        return dao;
    }


    @Override
    protected void beforeSave(ProjectTaskEntity obj) {
        String sysId = tableService.updateMaxSysId("t_gj_project_task", null);
        obj.setSysId(sysId);
    }

    public   ProjectTaskEntity saveTask(String projectId,int projectType){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = (calendar.get(Calendar.MONTH))+1;
        String taskName ="";
        String taskDate ="";
        switch (projectType){
            case 1://征迁
                ZhengQianEntity entity = zhengQianService.getById(projectId);
                taskName = "【"+entity.getProjectName()+"】";

                break;
            case 2: //开工提速
                KaiGongEntity kaigong = kaiGongService.getById(projectId);
                taskName="【"+kaigong.getProjectName()+"】";
                break;
            case 3:
                ZaijianTisuEntity zaijian = zaijianTisuService.getById(projectId);
                taskName="【"+zaijian.getProjectName()+"】";
                break;
            case 4:
                JungongTouchanEntity jungong = jungongTouchanService.getById(projectId);
                taskName="【"+jungong.getProjectName()+"】";
                break;
            case 5:
                ZhaoShangProjectEntity zhaoshang = zhaoshangService.getById(projectId);
                taskName="【"+zhaoshang.getProjectName()+"】";
                break;
            default:
                return  null;



        }
        if(day<=15){
            //上
            taskName=taskName+month+ "月上任务";
            taskDate=DateUtil.getCurrentMonth()+"-15";

        }else {
            //下
            taskName=taskName+month+ "月下任务";
            calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            taskDate = DateUtil.DateToStr(calendar.getTime());

        }
        ProjectTaskEntity taskEntity = new ProjectTaskEntity();
        taskEntity.setTaskName(taskName);
        taskEntity.setProjectId(projectId);
        taskEntity.setProjectType(projectType);
        taskEntity.setTaskEndDate(taskDate);
        taskEntity.setValid(1);//任务有效
        this.save(taskEntity);
        return taskEntity;
    }


    /**
     * 获取反馈信息
     * @param filter
     * @return
     */
    public List<ProjectFeedBackFilter> getListAll(ProjectTaskFilter filter,int prjectType)
    {
        List<ProjectFeedBackFilter> listData = new ArrayList<>();

        List<ProjectTaskEntity> list = dao.getList(filter);

        for (ProjectTaskEntity item : list) {
            List<ProjectFeedBackEntity> feedbacks = feedBackdao.getFeebackByName(item.getProjectId(), item.getSysId(), prjectType,null);
            listData = setListdata(listData, feedbacks, item, prjectType);


        }
        return listData;

    }

    /**
     * 只看自己的反馈情况
     * @param filter
     * @param prjectType
     * @return
     */
    public List<ProjectFeedBackFilter> getListByOwn(ProjectTaskFilter filter,int prjectType)
    {
        List<ProjectFeedBackFilter> listData = new ArrayList<>();
        String username = SysUserUtils.getusername();
        List<ProjectTaskEntity> list = dao.getList(filter);

        for (ProjectTaskEntity item : list) {
            List<ProjectFeedBackEntity> feedbacks = feedBackdao.getFeebackByName(item.getProjectId(), item.getSysId(), prjectType,username);
            listData = setListdata(listData, feedbacks, item, prjectType);


        }
        return listData;

    }



    public List<ProjectFeedBackFilter> setListdata(List<ProjectFeedBackFilter> listData,List<ProjectFeedBackEntity> feedbacks,ProjectTaskEntity item,int prjectType)
    {

        if(ObjectUtils.isNotBlank(feedbacks)){


            for (ProjectFeedBackEntity feedback : feedbacks) {



                ProjectFeedBackFilter dto = new ProjectFeedBackFilter();
                dto.setCreateName(feedback.getCreateName());
                dto.setXingxiang(feedback.getXingxiang());
                dto.setReachoals(feedback.getReachoals());
                dto.setTaskName(item.getTaskName());
                dto.setSysId(item.getSysId());
                dto.setCreateDate(feedback.getCreateDate()+" "+feedback.getCreateTime());
                dto.setTaskEndDate(item.getTaskEndDate());
                dto.setFeedbackId(feedback.getSysId());
                dto.setCompletion(feedback.getCompletion());
                dto.setAudit(feedback.getAudit());
                switch (prjectType) {
                    case 1:
                        dto.setRemarks(feedback.getRemarks());
                        dto.setCurrentMianji(feedback.getCurrentMianji());
                        dto.setFinishMianji(feedback.getFinishMianji());
                        dto.setCurrentChaiqian(feedback.getFinishChaiqian());
                        dto.setFinishChaiqian(feedback.getFinishChaiqian());
                        dto.setCurrentZhengqianhu(feedback.getCurrentZhengqianhu());
                        dto.setFinishZhengqianhu(feedback.getFinishZhengqianhu());
                        int zhengdiAudit = feedback.getZhengdiAudit();
                        String a1 = auditName(zhengdiAudit);
                        String a2 = auditName(feedback.getChaiqianAudit());
                        dto.setAuditInfo("征地审核："+a1+"\n"+"拆迁审核："+a2);
                        break;

                    case 2:
                        dto.setRemarks(feedback.getRemarks());
//                        dto.setAddCurrentValue(feedback.getAddCurrentValue());
                        dto.setCurrentValue(feedback.getCurrentValue());
                        dto.setTotalValue(feedback.getTotalValue());

                        break;

                    case 3:
                        dto.setRemarks(feedback.getRemarks());
                        dto.setCurrentValue(feedback.getCurrentValue());
                        dto.setTotalValue(feedback.getTotalValue());
                        dto.setAddCurrentValue(feedback.getAddCurrentValue());
                        break;


                    case 4:
//                        dto.setAddCurrentValue(feedback.getAddCurrentValue());
                        dto.setRemarks(feedback.getRemarks());
                        dto.setCurrentValue(feedback.getCurrentValue());
                        dto.setTotalValue(feedback.getTotalValue());
                        break;

                    case 5:
//                        dto.setAddCurrentValue(feedback.getAddCurrentValue());
                        dto.setRemarks(feedback.getRemarks());
                        dto.setCurrentValue(feedback.getCurrentValue());
                        dto.setTotalValue(feedback.getTotalValue());
                        break;

                }


                listData.add(dto);
            }
        }else {
            ProjectFeedBackFilter dto = new ProjectFeedBackFilter();
            dto.setTaskEndDate(item.getTaskEndDate());
            dto.setTaskName(item.getTaskName());
            dto.setSysId(item.getSysId());
            listData.add(dto);
        }

        return  listData;

    }


    private String auditName(int audit)
    {
        switch (audit)
        {
            case 0:

                return "未审核";

            case 1:
                return "已审核";

            case 2:
                return "审核不通过";
        }
        return "";
    }



    public List<ProjectTaskEntity> getListNoPage(ProjectTaskFilter filter)
    {

        List<ProjectTaskEntity> list = dao.getListNoPage(filter);

        return list;


    }



}
